<?php 
/* FlushAndContinue script, by Olivier St-Laurent
 * Version 1.0
 * 
 * Usage : 
 * 
 * FlushAndContinue::start(); // start the output buffer, needs to be called before ANY output
 * // Some HTML or JSON data..... 
 * FlushAndContinue::flush(); // Flush everything and close the connection to the browser
 * // Then you can write any PHP script you want in background, with no time limit. 
 * 
 */
class FlushAndContinue {
    static function start() {
        ob_start();
    }
    static function flush() {
        if(session_id())session_write_close();
        if (function_exists('apache_setenv')) {
            apache_setenv('no-gzip', 1);
        }
        @ini_set('zlib.output_compression', 0);
        @ini_set('implicit_flush', 1);
        ob_implicit_flush(1);
        ignore_user_abort(true);
        set_time_limit(0);
        header("Connection: close");
        if (function_exists('apache_setenv')) {
            header("Content-Length: ".ob_get_length());
        }
        ob_end_flush();
        ob_flush();
        flush();
        if (!function_exists('apache_setenv')) {
            fastcgi_finish_request();
        }
    }
}
